<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="公司主体">
          <el-select
            clearable
            v-model="form.sys_company_id"
            placeholder="公司主体"
          >
            <el-option
              v-for="item in companyListData"
              :key="item.id"
              :label="item.company_name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="账号类型">
          <el-select clearable v-model="form.card_type" placeholder="账号类型">
            <el-option key="receive" label="收款" value="receive"></el-option>
            <el-option key="send" label="付款" value="send"></el-option>
            <el-option key="both" label="收款+付款" value="both"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="账户名">
          <el-input v-model="form.name" placeholder="账户名" />
        </el-form-item>
        <el-form-item label="银行账号">
          <el-input v-model="form.card_number" placeholder="银行账号" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="company_name" label="主体" align="center" />
          <el-table-column prop="name" label="账户说明" align="center" />
          <el-table-column prop="card_type" label="账号类型" align="center">
            <template #default="scope">
              <el-tag
                v-if="scope.row.card_type"
                :type="
                  scope.row.card_type == 'send'
                    ? 'danger'
                    : scope.row.card_type == 'receive'
                    ? 'warning'
                    : 'success'
                "
              >
                {{
                  scope.row.card_type == "send"
                    ? "付款"
                    : scope.row.card_type == "receive"
                    ? "收款"
                    : "收款+付款"
                }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="is_active" label="是否可用" align="center">
            <template #default="scope">
              <el-tag :type="scope.row.is_active == 1 ? 'success' : 'danger'">
                {{ scope.row.is_active == 1 ? "可用" : "不可用" }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="bank" label="银行" align="center" />
          <el-table-column prop="card_number" label="银行账户" align="center" />
          <el-table-column prop="card_name" label="账户名" align="center" />
          <el-table-column prop="branch" label="支行" align="center" />
          <el-table-column prop="address" label="银行地址" align="center" />
          <el-table-column prop="swift_code" label="Swift码" align="center" />
          <el-table-column prop="currency" label="支持币种" align="center" />
          <el-table-column prop="country" label="国家" align="center" />
          <el-table-column prop="phone" label="预留手机号" align="center" />
          <el-table-column prop="email" label="预留邮箱" align="center" />
          <el-table-column
            prop="routing_number"
            label="路由号码"
            align="center"
          />
          <el-table-column prop="extended" label="补充信息" align="center" />
          <el-table-column
            prop="createtime"
            label="添加时间"
            align="center"
            width="180"
            :formatter="formatterTime"
          />
          <el-table-column
            label="操作"
            align="center"
            width="120"
            fixed="right"
          >
            <template #default="scope">
              <el-button
                type="primary"
                plain
                @click="handleOperate(scope.$index, scope.row, 'edit')"
                >编辑</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      :dialogFormVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
      :dialogTitle="dialogTitle"
      :companyListData="companyListData"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatterTime } from "@/utils/utils";
import {
  sysBankCardListApi,
  sysBankCardUpdateApi,
  sysCompanyListApi,
} from "@/api/ExAssistant/settings";
import DialogForm from "./components/dialogForm.vue";
const tableData = ref([]);
const loading = ref(false);
const dialogVisible = ref(false);
const btnLoading = ref(false);
const dialogTitle = ref("");
const companyListData = ref([]);
const formData = {
  name: "",
  sys_company_id: "",
  card_number: "",
  card_type: "",
};
const form = reactive({ ...formData });
const dialogFormData = {
  id: "",
  sys_company_id: "",
  card_type: "",
  is_active: "",
  name: "",
  bank: "",
  country: "",
  branch: "",
  address: "",
  card_name: "",
  card_number: "",
  swift_code: "",
  phone: "",
  email: "",
  currency: "",
  routing_number: "",
  extended: "",
};
const dialogForm = reactive({ ...dialogFormData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    name: form.name,
    sys_company_id: form.sys_company_id,
    card_number: form.card_number,
    card_type: form.card_type,
  };
};

// 获取表格数据
const getTableData = async () => {
  loading.value = true;

  try {
    const res = await sysBankCardListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 获取表格数据
const getCompanyData = async () => {
  loading.value = true;
  try {
    const res = await sysCompanyListApi({ page: 1, pagesize: 100 });
    loading.value = false;
    companyListData.value = res.data.results;
  } catch (error) {
    loading.value = false;
  }
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
  getTableData();
};
// 查询
const onQuery = async () => {
  getTableData();
};
//新增
const onAdd = () => {
  dialogTitle.value = "新增系统收款银行";
  dialogVisible.value = true;
  Object.assign(dialogForm, { ...dialogFormData });
};
const handleOperate = async (index, row, type) => {
  // 编辑
  if (type === "edit") {
    dialogTitle.value = "编辑系统收款银行";
    Object.assign(dialogForm, { ...row });
    dialogVisible.value = true;
  }
};
//关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;
  Object.assign(dialogForm, { ...dialogFormData });
};

// 取消
const cancel = () => {
  dialogVisible.value = false;
  Object.assign(dialogForm, { ...dialogFormData });
};
// 提交
const submit = async (param) => {
  try {
    btnLoading.value = true;
    const res = await sysBankCardUpdateApi(param);
    ElMessage({
      message: res.message,
      type: "success",
    });
    dialogVisible.value = false;
    btnLoading.value = false;
    getTableData();
  } catch (error) {
    btnLoading.value = false;
    dialogVisible.value = false;
  }
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTableData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTableData();
};
onMounted(() => {
  getTableData();
  getCompanyData();
});
</script>
